package gx.entrance.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import gx.JsonResult;
import gx.entrance.entity.User;
import gx.entrance.service.UserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("entrance")
@Api(value = "Entrance",description = "入口 针对user用户操作表")
public class EntranceController {

	@Autowired
	private UserService userService;
	
	// 创建线程安全的Map
	static Map<String, User> users = Collections.synchronizedMap(new HashMap<String, User>());
	
	// log4j日志输出
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
    /**
     * 测试入口,Spring boot 标准模式
     * @return
     */
	@ApiOperation(value="入口测试方法", notes="类初始化")
    @RequestMapping(value="/entranceStart" , method = RequestMethod.GET)
    public String entranceStart(){
        return "ok.DemoController.save";
    }
	
	/**
	 * 添加用户
	 * @param user
	 * @return
	 */
	@ApiOperation(value="创建用户", notes="根据User对象创建用户")
	@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
	@RequestMapping(value = "user", method = RequestMethod.POST)
	public ResponseEntity<JsonResult> add (@RequestBody User user){
		JsonResult r = new JsonResult();
		try {
			userService.add(user);
			users.put(user.getUuid(), user);
			r.setResult(user.getUuid());
			r.setStatus("ok");
		} catch (Exception e) {
			r.setResult(e.getClass().getName() + ":" + e.getMessage());
			r.setStatus("error");

			e.printStackTrace();
		}
		return ResponseEntity.ok(r);
	}
	
	/**
	 * 根据ID查询用户
	 * @param id
	 * @return
	 */
	@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
	@ApiImplicitParam(name = "uuid", value = "用户ID", required = true, dataType = "String", paramType = "path")
	@RequestMapping(value = "user/{uuid}" , method = RequestMethod.GET)
	public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "uuid")String uuid){
		JsonResult r = new JsonResult();
		try {
			User user = userService.getUserById(uuid);
			r.setResult(user);
			r.setStatus("ok");
		} catch (Exception e) {
			r.setResult(e.getClass().getName() + ":" + e.getMessage());
			r.setStatus("error");
			e.printStackTrace();
		}
		return ResponseEntity.ok(r);
	}
	
	/**
	 * 根据id删除用户
	 * @param uuid
	 * @return
	 */
	@ApiOperation(value="删除用户", notes="根据url的id来指定删除用户")
	@ApiImplicitParam(name = "uuid", value = "用户ID", required = true, dataType = "Long", paramType = "path")
	@RequestMapping(value = "user/{uuid}", method = RequestMethod.DELETE)
	public ResponseEntity<JsonResult> delete (@PathVariable(value = "uuid")String uuid){
		JsonResult r = new JsonResult();
		try {
			userService.delete(uuid);
			r.setResult(uuid);
			r.setStatus("ok");
		} catch (Exception e) {
			r.setResult(e.getClass().getName() + ":" + e.getMessage());
			r.setStatus("error");
			e.printStackTrace();
		}
		return ResponseEntity.ok(r);
	}
	
	/**
	 * 查询用户列表
	 * @return
	 */
	@ApiOperation(value="获取用户列表", notes="获取用户列表")
	@RequestMapping(value = "users", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> getUserList (){
		JsonResult r = new JsonResult();
		try {  
			List<User> userList = userService.getUserList();
			r.setResult(userList);
			r.setStatus("ok");
		} catch (Exception e) {
			r.setResult(e.getClass().getName() + ":" + e.getMessage());
			r.setStatus("error");
			e.printStackTrace();
		}
		return ResponseEntity.ok(r);
	}
	
	/**
	 * 根据id修改用户信息
	 * @param user
	 * @return
	 */
	@ApiOperation(value="更新信息", notes="根据url的id来指定更新用户信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "uuid", value = "用户ID", required = true, dataType = "Long",paramType = "path"),
			@ApiImplicitParam(name = "user", value = "用户实体user", required = true, dataType = "User")
	})
	@RequestMapping(value = "user/{uuid}", method = RequestMethod.PUT)
	public ResponseEntity<JsonResult> update (@PathVariable("uuid") String uuid, @RequestBody User user){
		JsonResult r = new JsonResult();
		try {
			userService.update(uuid, user);
			r.setResult(user);
			r.setStatus("ok");
		} catch (Exception e) {
			r.setResult(e.getClass().getName() + ":" + e.getMessage());
			r.setStatus("error");

			e.printStackTrace();
		}
		return ResponseEntity.ok(r);
	}
	
	
	/**
	 * 
		{	
			"uuid": "swagger",
			"login_name": "swagger",
			"login_password": "swagger111",
			"nick_name": "nick_name",
			"status": 1,
			"add_time": "add_time",
			"remark": "remark"
		}
	 */
}
